﻿Imports CrystalDataHub.BLL
Imports Infragistics.Win.UltraWinGrid
Imports Crystal.CLL
Imports CrystalDataHub.BLL.NewColumnManager
Imports CrystalCommon.View
Imports System.Text
Imports CrystalDataHub.BLL.DataHub.BusinessObject
Imports System.Data.SqlTypes
Imports CrystalDataHub.BLL.CrystalDataHub.BLL

Public Class AddNewColumn
#Region "Declare"
    Dim mclsNewColumnManager As New NewColumnManager
    Public mintProjectID As Integer
    Private marrListingColumns As String() = {"Select", "Listing", "Column Title", "ND", "NIL", "ListingID"}
    Private mboolDataSaved As Boolean = False
    Public mlstNewColumns As List(Of String)
    Private mstrDate As String = "Date"
    Private mstrFilingDate As String = "Filing Date"
    Private mstrsingleSpace As String = " "
    Private mblnLoadProcessing As Boolean = False
    Public mstrSourceTypeCode As String
    Public mblnColumnList As Boolean = False
    Public mdtGridColumns As DataTable
    Public mlstNewAddedColumns As List(Of CurrentDataSources)
    Public mblnFLScreen As Boolean = True
    Public mblnCloseNoChange As Boolean = False
    Public mstrDataSource As String
#End Region

#Region "Form Load"

    Private Sub AddNewColumn_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If mboolDataSaved Then
            Me.Dispose()
        Else
            If (MessageBox.Show(MessageManager.GetMessage(DBResult.COLUMN_EXIT_SAVE.ToString), BaseClass.crystalCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.No) Then
                e.Cancel = True
            Else
                mblnCloseNoChange = True
            End If
        End If
    End Sub

    ''' <summary>
    ''' Form Load
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub AddNewColumn_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadFunctionality()
    End Sub

    ''' <summary>
    ''' cmbData source 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmbDataSource_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles cmbDataSource.InitializeLayout
        e.Layout.Bands(0).Columns("SourceTypeID").Hidden = True
        e.Layout.Bands(0).Columns("SourceTypeCode").Hidden = True
        e.Layout.Bands(0).Columns("SourceTypeID").CellDisplayStyle = CellDisplayStyle.PlainText
        e.Layout.Bands(0).Columns("SourceTypeCode").CellDisplayStyle = CellDisplayStyle.PlainText
        e.Layout.Bands(0).Columns("SourceTypeName").Header.Caption = "Data Source"
        e.Layout.Bands(0).Columns("SourceTypeName").Width = cmbDataSource.Width
    End Sub

    ''' <summary>
    ''' Load the values of source type and Load the column of grid on data hub. i.e. already exists.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmbDataSource_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbDataSource.ValueChanged
        If cmbDataSource Is Nothing Then Exit Sub
        If cmbDataSource.Value IsNot Nothing Then
            If cmbDataSource.SelectedRow.GetCellValue("SourceTypeCode") = SourceTypeCode.BKL.ToString Or _
                cmbDataSource.SelectedRow.GetCellValue("SourceTypeCode") = SourceTypeCode.RGT.ToString Or _
                cmbDataSource.SelectedRow.GetCellValue("SourceTypeCode") = SourceTypeCode.PRB.ToString Then
                dpSourceDate.Enabled = True
                'RequiredDateTimeControl(dpSourceDate, lblSDate.Text)
            Else
                dpSourceDate.Enabled = False
                'RequiredDateTimeControl(dpSourceDate, lblSDate.Text, False)
            End If
            mblnLoadProcessing = True
            LoadSourceType(cmbDataSource.Value)
            LoadColumnListing(cmbDataSource.Value)
            mblnLoadProcessing = False
        End If
    End Sub

    Private Sub SetPropertiesOfGrid(ByVal lstrSourceType As String)
        If lstrSourceType = SourceTypeCode.UPD.ToString Then
            grdColumnListing.DisplayLayout.Bands(0).Columns("Select").CellActivation = False
            grdColumnListing.DisplayLayout.Bands(0).Columns("IsND").CellActivation = False
            grdColumnListing.DisplayLayout.Bands(0).Columns("IsNIL").CellActivation = False
        End If
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmbSourceName_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles cmbSourceName.InitializeLayout
        e.Layout.Bands(0).Columns("ESourceID").Hidden = True
        e.Layout.Bands(0).Columns("ESourceID").CellDisplayStyle = CellDisplayStyle.PlainText
        e.Layout.Bands(0).Columns("ESourceName").Header.Caption = "Source Name"
        e.Layout.Bands(0).Columns("ESourceName").Width = cmbSourceName.Width
        e.Layout.Bands(0).Columns("ESourceCode").Hidden = True
        e.Layout.Bands(0).Columns("ESourceCode").CellDisplayStyle = CellDisplayStyle.PlainText
    End Sub
#End Region

#Region "Methods and Functions"

    Private Sub LoadFunctionality()
        Me.Text = TitleManager.GetTitle("AddNewColumn")
        SetComboValueMember()
        LoadDataSource()
        SetProperties()
        LoadListingGridColumns()
        LoadExistingColumnsInGrid()
    End Sub

    Private Sub LoadExistingColumnsInGrid()
        Dim ldtExistingcolumn As DataTable
        Try
            ldtExistingcolumn = New DataTable
            ldtExistingcolumn.Columns.Add("Already in Grid", GetType(String))
            grdColumnDataHub.DataSource = ldtExistingcolumn
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            ldtExistingcolumn = Nothing
        End Try
    End Sub

    Private Sub SetProperties()
        Me.grdColumnDataHub.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
        Me.grdColumnListing.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
        Me.grdColumnDataHub.DisplayLayout.GroupByBox.Hidden = True
        Me.grdColumnListing.DisplayLayout.GroupByBox.Hidden = True
    End Sub

    ''' <summary>
    ''' Load data sources from db.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadDataSource()
        Dim ldtDataSource As DataTable
        Try
            ldtDataSource = New DataTable
            ldtDataSource = mclsNewColumnManager.GetColumnSourceType()
            cmbDataSource.DataSource = ldtDataSource
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            ldtDataSource = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' Load the source type 
    ''' </summary>
    ''' <param name="SourceTypeCode"></param>
    ''' <remarks></remarks>
    Private Sub LoadSourceType(ByVal SourceTypeCode As String)
        Dim ldsSourceType As DataSet
        Try
            ldsSourceType = New DataSet
            ldsSourceType = mclsNewColumnManager.GetColumnDataSource(SourceTypeCode, mintProjectID, mblnFLScreen)
            cmbSourceName.DataSource = ldsSourceType.Tables(ColumnDataSource.CmbSourceName)
            mdtGridColumns = ldsSourceType.Tables(ColumnDataSource.GrdSourceName)
            SetSourceNameValues(SourceTypeCode)
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            ldsSourceType = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' Set the default values of source type on change of data source.
    ''' </summary>
    ''' <param name="lstrSourceTypeCode"></param>
    ''' <remarks></remarks>
    Private Sub SetSourceNameValues(ByVal lstrSourceTypeCode As String)
        If cmbSourceName.DataSource Is Nothing Then Exit Sub
        Dim ldtSourceName As DataTable
        Try
            If lstrSourceTypeCode = SourceTypeCode.UPD.ToString Then
                If cmbSourceName.Rows.Count <> 0 Then
                    cmbSourceName.Value = cmbSourceName.Rows(0).GetCellValue("ESourceID")
                    grdColumnDataHub.DataSource = mdtGridColumns
                End If
            ElseIf lstrSourceTypeCode = SourceTypeCode.DDI.ToString Then
                cmbSourceName.Value = cmbSourceName.Rows(0).GetCellValue("ESourceID")
                grdColumnDataHub.DataSource = mdtGridColumns
            ElseIf cmbDataSource.Value = SourceTypeCode.BKL.ToString Or cmbDataSource.Value = SourceTypeCode.EBL.ToString Or cmbDataSource.Value = SourceTypeCode.PRB.ToString Or cmbDataSource.Value = SourceTypeCode.REG.ToString Then
                ldtSourceName = New DataTable
                ldtSourceName = CType(cmbSourceName.DataSource, DataTable)
                'Add --New-- option to combo box at zero index.
                CommonCLL.AddAtFirstIndexInDropDown(ldtSourceName, cmbSourceName.ValueMember, cmbSourceName.DisplayMember, CommonCLL.mintNewOptionID, CommonCLL.m_strNewTextValue)
            Else
                ldtSourceName = New DataTable
                ldtSourceName = CType(cmbSourceName.DataSource, DataTable)
                'Add --New-- option to combo box at zero index.
                CommonCLL.AddAtFirstIndexInDropDown(ldtSourceName, cmbSourceName.ValueMember, cmbSourceName.DisplayMember, CommonCLL.mintNewOptionID, CommonCLL.m_strNewTextValue)
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            ldtSourceName = Nothing
        End Try
    End Sub

    Private Sub SetComboValueMember()
        cmbDataSource.ValueMember = "SourceTypeCode"
        cmbDataSource.DisplayMember = "SourceTypeName"

        cmbSourceName.ValueMember = "ESourceID"
        cmbSourceName.DisplayMember = "ESourceName"
    End Sub

    ''' <summary>
    ''' Add columns, display on grid to add new colunmn.
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private Sub LoadListingGridColumns()
        Dim ldtListing As DataTable
        Try
            ldtListing = New DataTable
            ldtListing.Columns.Add(marrListingColumns(0), GetType(Boolean))
            ldtListing.Columns.Add(marrListingColumns(1), GetType(String))
            ldtListing.Columns.Add(marrListingColumns(2), GetType(String))
            ldtListing.Columns.Add(marrListingColumns(3), GetType(Boolean))
            ldtListing.Columns.Add(marrListingColumns(4), GetType(Boolean))
            ldtListing.Columns.Add(marrListingColumns(5), GetType(String))
            grdColumnListing.DataSource = ldtListing
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            ldtListing = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' Set values of grid, on selection of data source.
    ''' </summary>
    ''' <param name="lstrDataSource"></param>
    ''' <remarks></remarks>
    Private Sub LoadColumnListing(ByVal lstrDataSource As String)
        Dim ldtGridListings As DataTable
        Dim ldrGridListing As DataRow
        Dim ldtProjectListing As DataTable
        Try
            ldtGridListings = New DataTable
            ldtProjectListing = New DataTable
            ldtGridListings = CType(grdColumnListing.DataSource, DataTable)
            'Clear the previous values.
            ldtGridListings.Clear()
            'Retrieve the listing name and listid from database.
            ldtProjectListing = mclsNewColumnManager.GetProjectListing(mintProjectID)
            For i As Integer = 0 To ldtProjectListing.Rows.Count - 1
                ldrGridListing = ldtGridListings.NewRow()
                'Getselectvalue set the defaut value of checkboxes of listing,.*/
                ldrGridListing(marrListingColumns(0)) = GetSelectValue(lstrDataSource)
                ldrGridListing(marrListingColumns(1)) = ldtProjectListing.Rows(i)("ListingName")
                ldrGridListing(marrListingColumns(2)) = String.Empty
                ldrGridListing(marrListingColumns(3)) = False
                ldrGridListing(marrListingColumns(4)) = False
                ldrGridListing(marrListingColumns(5)) = ldtProjectListing.Rows(i)("ListingID")
                'Adding to ldtGridListings because this is the datasource of grid.
                ldtGridListings.Rows.Add(ldrGridListing)
            Next
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            ldtGridListings = Nothing
            ldtProjectListing = Nothing
            ldrGridListing = Nothing
        End Try

    End Sub

    ''' <summary>
    ''' Get select value set the defaut value of checkboxes of listing,.
    ''' </summary>
    ''' <param name="strSourceType"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetSelectValue(ByVal strSourceType As String) As Boolean
        Dim lboolValue As Boolean = False
        If strSourceType = SourceTypeCode.UPD.ToString Then
            lboolValue = True
        ElseIf strSourceType = SourceTypeCode.DDI.ToString Then
            lboolValue = False
        ElseIf strSourceType = SourceTypeCode.PBC.ToString Then
            lboolValue = False
        Else
            lboolValue = False
        End If
        Return lboolValue
    End Function

    ''' <summary>
    ''' Check blank column title. 
    ''' </summary>
    ''' <param name="ldtListing"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckBlankColumnTitle(ByVal ldtListing As DataTable) As Boolean
        For Each row As DataRow In ldtListing.Rows
            If row("Select") = True Then
                If row("Column Title").ToString = String.Empty Then
                    Me.ShowMessageBox(DBResult.NEW_COLUMN_BLANK)
                    Return False
                ElseIf String.Compare(row("Column Title"), mstrUpdatesColPrefix.Trim, True) = 0 OrElse _
                        String.Compare(row("Column Title"), mstrDDInfoColPrefix.Trim, True) = 0 OrElse _
                        String.Compare(row("Column Title"), mstrRegisterColPrefix.Trim, True) = 0 OrElse _
                        String.Compare(row("Column Title"), mstrDate.Trim, True) = 0 OrElse _
                        String.Compare(row("Column Title"), mstrFilingDate.Trim, True) = 0 OrElse _
                        String.Compare(row("Column Title"), mstrBanklistColPrefix.Trim, True) = 0 Then
                    Me.ShowMessageBox(DBResult.EDS_SYS_COL_NAME, row("Column Title") + " " + row("Listing"))
                    Return False
                End If
            End If
        Next
        Return True
    End Function

    ''' <summary>
    ''' CrEATE xml from datatable.
    ''' </summary>
    ''' <param name="ldtColumnTitle"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected Function CreateListingXML(ByVal ldtColumnTitle As DataTable) As StringBuilder
        Dim i As Integer
        Dim sbXML As New StringBuilder
        Try
            If ldtColumnTitle.Rows.Count > 0 Then
                sbXML.Append("<Root>")
                For i = 0 To ldtColumnTitle.Rows.Count - 1
                    If Not CommonCLL.blnRowBlank(ldtColumnTitle.Rows(i)) Then
                        If ldtColumnTitle.Rows(i)("Select") = True Then
                            sbXML.Append("<Row SourceName=""" & ldtColumnTitle.Rows(i)("Column Title").ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", _
                                         "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                     "ListingID=""" & ldtColumnTitle.Rows(i)("ListingID").ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                     "ND=""" & ldtColumnTitle.Rows(i)("ND").ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                     "NIL=""" & ldtColumnTitle.Rows(i)("NIL").ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & "/>")
                            sbXML.Append(vbNewLine)
                        End If
                    End If
                Next
                sbXML.Append("</Root>")
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        End Try
        Return sbXML
    End Function

    ''' <summary>
    ''' Check if the column title already exist in Grid of Data Hub.
    ''' </summary>
    ''' <param name="pstrColumnTitle"></param>
    ''' <param name="pstrListing"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckTitleInDataHub(ByVal pstrColumnTitle As String, ByVal pstrListing As String) As Boolean
        Dim ldtGridListings As DataTable
        Dim foundRows() As DataRow
        Dim expression As String
        Try
            ldtGridListings = New DataTable
            ldtGridListings = CType(grdColumnDataHub.DataSource, DataTable)
            If pstrListing = String.Empty Then
                expression = "[Already In Grid] ='" + pstrColumnTitle + "'"
            Else
                expression = "[Already In Grid] ='" + pstrColumnTitle + mstrsingleSpace + pstrListing + "'"
            End If
            foundRows = ldtGridListings.Select(expression)
            If foundRows.Length > 0 Then
                MessageBox.Show(MessageManager.GetMessage(DBResult.COLUMN_TITLE_EXIST.ToString), BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            foundRows = Nothing
            ldtGridListings = Nothing
        End Try
        Return True
    End Function
#End Region

#Region "Events"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdColumnListing_BeforeCellActivate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CancelableCellEventArgs) Handles grdColumnListing.BeforeCellActivate
        If cmbSourceName.Text.ToString = String.Empty Then
            MessageBox.Show(MessageManager.GetMessage(DBResult.DE_SELECT_SOURCENAME.ToString), BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
            cmbSourceName.Focus()
            Exit Sub
        End If

        If cmbDataSource.Value = SourceTypeCode.UPD.ToString Then
            If e.Cell.Column.Header.Caption = "Listing" Or e.Cell.Column.Header.Caption = "ND" _
                Or e.Cell.Column.Header.Caption = "NIL" Or e.Cell.Column.Header.Caption = "Select" Then
                e.Cell.Activation = Activation.Disabled
            End If
        ElseIf cmbDataSource.Value = SourceTypeCode.DDI.ToString Then
            If e.Cell.Column.Header.Caption = "Listing" Or e.Cell.Column.Header.Caption = "ND" _
                Or e.Cell.Column.Header.Caption = "NIL" Then
                e.Cell.Activation = Activation.Disabled
            End If
        ElseIf cmbDataSource.Value = SourceTypeCode.PBC.ToString Then
            If e.Cell.Column.Header.Caption = "Listing" Or e.Cell.Column.Header.Caption = "ND" _
                Or e.Cell.Column.Header.Caption = "NIL" Then
                e.Cell.Activation = Activation.Disabled
            End If
        ElseIf cmbDataSource.Value = SourceTypeCode.BKL.ToString Or cmbDataSource.Value = SourceTypeCode.EBL.ToString Or cmbDataSource.Value = SourceTypeCode.PRB.ToString Or cmbDataSource.Value = SourceTypeCode.RGT.ToString Then
            If e.Cell.Row.GetCellValue("Select") = True Then
                If e.Cell.Column.Header.Caption = "Listing" Then
                    e.Cell.Activation = Activation.Disabled
                End If
                If e.Cell.Column.Header.Caption = "ND" Then
                    e.Cell.Row.Cells("NIL").Value = False
                    'Clicked cell is NIL. If cell values is false.Set to true. 
                    'And If value is false.Check value of "ND" if "ND" is true than set the "ND" to false and true to "NIL"
                ElseIf e.Cell.Column.Header.Caption = "NIL" Then
                    e.Cell.Row.Cells("ND").Value = False
                End If
            Else
                If e.Cell.Column.Header.Caption = "ND" Or e.Cell.Column.Header.Caption = "NIL" Then
                    e.Cancel = True
                End If
            End If
            grdColumnListing.ActiveRow.Cells("Select").Column.CellClickAction = CellClickAction.Edit
        Else
            grdColumnListing.ActiveRow.Cells("Select").Column.CellClickAction = CellClickAction.Edit
        End If
    End Sub

    ''' <summary>
    ''' Check the column name if already exist than cancel update.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub grdColumnListing_BeforeCellDeactivate(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles grdColumnListing.BeforeCellDeactivate
        If grdColumnListing.ActiveCell.Column.Header.Caption = "Column Title" Then
            'Pass the column title and listing. Check if exist than canel changes.
            If CheckTitleInDataHub(grdColumnListing.ActiveCell.Row.GetCellValue(grdColumnListing.ActiveCell.Column).ToString, _
                                   grdColumnListing.ActiveCell.Row.GetCellValue("Listing").ToString) = False Then
                e.Cancel = True
            Else
                If mblnFLScreen And grdColumnListing.ActiveRow.Index = 0 Then
                    For Each row As UltraGridRow In grdColumnListing.Rows
                        If row.Index > 0 Then
                            row.Cells("Column Title").Value = grdColumnListing.ActiveCell.Value
                        End If
                    Next
                End If
            End If
        End If
    End Sub

    Private Sub grdColumnListing_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdColumnListing.InitializeLayout
        e.Layout.Bands(0).Columns("ListingID").Hidden = True
        e.Layout.Bands(0).Columns("ListingID").CellDisplayStyle = CellDisplayStyle.PlainText


    End Sub

    ''' <summary>
    ''' set the default properties of grid as per specifications.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdColumnListing_InitializeRow(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeRowEventArgs) Handles grdColumnListing.InitializeRow
        If e.ReInitialize = False Then
            If cmbDataSource.Value = SourceTypeCode.UPD.ToString Then
                e.Row.Cells("Select").Column.CellActivation = Activation.Disabled '.cellactiva.EditorComponent = 
                e.Row.Cells("Listing").Column.CellActivation = Activation.Disabled
                e.Row.Cells("Column Title").Column.CellClickAction = CellClickAction.Edit
                e.Row.Cells("ND").Column.CellActivation = Activation.Disabled '.cellactiva.EditorComponent = 
                e.Row.Cells("NIL").Column.CellActivation = Activation.Disabled '.cellactiva.EditorComponent = 
            ElseIf cmbDataSource.Value = SourceTypeCode.DDI.ToString Then
                e.Row.Cells("Select").Column.CellActivation = Activation.AllowEdit
                e.Row.Cells("Select").Column.CellClickAction = CellClickAction.Edit
                e.Row.Cells("Listing").Column.CellActivation = Activation.Disabled
                e.Row.Cells("Column Title").Column.CellClickAction = CellClickAction.Edit
                e.Row.Cells("ND").Column.CellActivation = Activation.Disabled '.cellactiva.EditorComponent = 
                e.Row.Cells("NIL").Column.CellActivation = Activation.Disabled '.cellactiva.EditorComponent =
            ElseIf cmbDataSource.Value = SourceTypeCode.PBC.ToString Then
                e.Row.Cells("Select").Column.CellActivation = Activation.AllowEdit
                e.Row.Cells("Select").Column.CellClickAction = CellClickAction.Edit
                e.Row.Cells("Listing").Column.CellActivation = Activation.Disabled
                e.Row.Cells("Column Title").Column.CellClickAction = CellClickAction.Edit
                e.Row.Cells("ND").Column.CellActivation = Activation.Disabled '.cellactiva.EditorComponent = 
                e.Row.Cells("NIL").Column.CellActivation = Activation.Disabled '.cellactiva.EditorComponent =
            ElseIf cmbDataSource.Value = SourceTypeCode.EBL.ToString Or cmbDataSource.Value = SourceTypeCode.BKL.ToString Or cmbDataSource.Value = SourceTypeCode.PRB.ToString Or cmbDataSource.Value = SourceTypeCode.RGT.ToString Then
                e.Row.Cells("Select").Column.CellActivation = Activation.AllowEdit
                e.Row.Cells("Select").Column.CellClickAction = CellClickAction.Edit
                e.Row.Cells("Listing").Column.CellActivation = Activation.Disabled
                e.Row.Cells("Column Title").Column.CellClickAction = CellClickAction.Edit
                e.Row.Cells("ND").Column.CellActivation = Activation.AllowEdit
                e.Row.Cells("NIL").Column.CellActivation = Activation.AllowEdit
                e.Row.Cells("ND").Column.CellClickAction = CellClickAction.Edit
                e.Row.Cells("NIL").Column.CellClickAction = CellClickAction.Edit
            Else
                e.Row.Cells("Select").Column.CellActivation = Activation.AllowEdit
                e.Row.Cells("Listing").Column.CellActivation = Activation.Disabled
                e.Row.Cells("Column Title").Column.CellActivation = Activation.AllowEdit
                e.Row.Cells("ND").Column.CellActivation = Activation.Disabled '.cellactiva.EditorComponent = 
                e.Row.Cells("NIL").Column.CellActivation = Activation.Disabled '.cellactiva.EditorComponent =
            End If
        End If
    End Sub

    ''' <summary>
    ''' Event fires when the Source name changed.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmbSourceName_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbSourceName.ValueChanged
        Dim lfrmAddValue As AddValue
        Dim ldtDataSource As DataTable
        Dim foundRows() As DataRow
        Dim expression As String
        Dim Id As String
        Dim lstrSelectedDataSource As String = String.Empty
        Try
            If mblnLoadProcessing = False Then
                If cmbSourceName.Value Is Nothing Then Exit Sub
                grdColumnDataHub.DataSource = mdtGridColumns
                lstrSelectedDataSource = cmbDataSource.SelectedRow.GetCellValue("SourceTypeCode")
                If lstrSelectedDataSource <> SourceTypeCode.UPD.ToString AndAlso lstrSelectedDataSource <> SourceTypeCode.DDI.ToString Then
                    'Show dialog box when select value is new to add new source name.
                    If cmbSourceName.Value = CommonCLL.mintNewOptionID Then
                        lfrmAddValue = New AddValue(enmEntityType.Source.ToString(), LKPMaster.EDSDSN, "Source Name")
                        lfrmAddValue.StartPosition = FormStartPosition.CenterScreen
                        lfrmAddValue.ParameterInt = cmbDataSource.SelectedRow.GetCellValue("SourceTypeID")
                        lfrmAddValue.ShowDialog()
                        Id = lfrmAddValue.ID
                        If Id <> String.Empty Then
                            ldtDataSource = New DataTable
                            ldtDataSource = cmbDataSource.DataSource
                            'Search if id already exist in combo box.
                            expression = "SourceTypeID ='" + Trim(Id).Replace("'", "''") + "'"
                            foundRows = ldtDataSource.Select(expression)
                            'If Id not exist in combo than add to the combo.
                            If foundRows.Length = 0 Then
                                If CommonCLL.DropDownAtLast(cmbSourceName.DataSource, cmbSourceName.ValueMember, cmbSourceName.DisplayMember, lfrmAddValue.ID, lfrmAddValue.mstrAddedValue) = False Then
                                    CommonCLL.ShowMessage("Error in accessing Source Name drop down.", MessageBoxIcon.Error)
                                    Exit Sub
                                End If
                            End If
                            cmbSourceName.Value = lfrmAddValue.ID
                        Else
                            cmbSourceName.Value = String.Empty
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            ldtDataSource = Nothing
            lfrmAddValue = Nothing
            foundRows = Nothing
        End Try
    End Sub

    Private Sub MakeEntityColumns(ByVal drRow As DataRow, ByRef dtNewColumns As DataTable)
        Dim ldrListingRow As DataRow
        ldrListingRow = dtNewColumns.NewRow()
        ldrListingRow("Select") = drRow("Select")
        ldrListingRow("Listing") = drRow("Listing").ToString
        ldrListingRow("Column Title") = drRow("Column Title").ToString + mstrsingleSpace + drRow("Listing").ToString
        ldrListingRow("ListingID") = drRow("ListingID")
        ldrListingRow("ND") = drRow("ND")
        ldrListingRow("NIL") = drRow("NIL")
        dtNewColumns.Rows.Add(ldrListingRow)
        'If cmbDataSource.Value = SourceTypeCode.UPD.ToString Or cmbDataSource.Value = SourceTypeCode.DDI.ToString Then
        '    If cmbSourceName.Value = DataSourceCode.DDALCOT Then Exit Sub
        '    ldrListingRow = dtNewColumns.NewRow()
        '    ldrListingRow("Select") = drRow("Select")
        '    ldrListingRow("Listing") = drRow("Listing").ToString
        '    ldrListingRow("Column Title") = drRow("Column Title").ToString + mstrsingleSpace + drRow("Listing").ToString + " Date"
        '    ldrListingRow("ListingID") = drRow("ListingID")
        '    ldrListingRow("ND") = drRow("ND")
        '    ldrListingRow("NIL") = drRow("NIL")
        '    dtNewColumns.Rows.Add(ldrListingRow)
        'End If
    End Sub

    ''' <summary>
    ''' Add New Column
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim ldtListing As DataTable
        Dim lstrColumns As String = String.Empty
        Dim lintMessageId As Integer
        Dim ldtListingTable As DataTable = Nothing
        Dim ldrListingRow As DataRow
        Dim dataView As DataView
        Dim lintESourceID As Integer
        Dim lstrSourceTypeCode As String
        Dim lstrSpaceIntend = "    "
        'Dim lstCurrentDataSources As CurrentDataSources
        Try
            grdColumnListing.UpdateData()
            ldtListing = CType(grdColumnListing.DataSource, DataTable)
            lintESourceID = cmbSourceName.Value
            lstrSourceTypeCode = cmbDataSource.Value
            ' Exit if the column title is blank but checkbox marked true.
            If CheckBlankColumnTitle(ldtListing) = False Then
                Exit Sub
            End If
            ' ldtListingTable 'll contain only column title which are marked true.

            ldtListingTable = New DataTable
            ldtListingTable = ldtListing.Clone()
            If mlstNewAddedColumns Is Nothing Then
                mlstNewAddedColumns = New List(Of CurrentDataSources)
            End If


            For Each ldrow As DataRow In ldtListing.Rows
                If ldrow("Select") = True Then
                    If CheckTitleInDataHub(ldrow("Column Title").ToString, ldrow("Listing")) Then
                        MakeEntityColumns(ldrow, ldtListingTable)
                        GetNewEntityColumnsList(ldrow)
                    End If
                End If
            Next

            ''Create XML from datatable to store the new created columns.
            lstrColumns = CreateListingXML(ldtListingTable).ToString
            If lstrColumns <> String.Empty Then
                lintMessageId = mclsNewColumnManager.InsertNewColumn(lstrColumns, lintESourceID, mintProjectID, CommonCLL.UserID, If(mblnFLScreen, True, False) _
                                                                     , If(dpSourceDate.Enabled, dpSourceDate.Value, SqlDateTime.Null))
                If lintMessageId = DBResult.NEW_COLUMN_ADDED Then
                    MessageBox.Show(MessageManager.GetMessage(DBResult.NEW_COLUMN_ADDED.ToString), BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    mboolDataSaved = True
                End If
            End If

            If ldtListingTable IsNot Nothing AndAlso ldtListingTable.Rows.Count > 0 Then
                mstrSourceTypeCode = cmbDataSource.Value
                mstrDataSource = cmbDataSource.SelectedRow.GetCellValue("SourceTypeName")
                Me.Close()
            Else
                If ldtListingTable IsNot Nothing And ldtListingTable.Rows.Count = 0 Then
                    MessageBox.Show("Please select atleast one Column Title.", BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            ldtListing = Nothing
            ldtListingTable = Nothing
            ldrListingRow = Nothing
            dataView = Nothing
        End Try
    End Sub

    Private Sub GetNewEntityColumnsList(ByVal ldrow As DataRow)
        Dim lstrSpaceIntend = "    "
        Dim lstCurrentDataSources As CurrentDataSources
        lstCurrentDataSources = New CurrentDataSources
        lstCurrentDataSources.ListingName = ldrow("Listing")
        lstCurrentDataSources.ESourceCode = cmbSourceName.SelectedRow.GetCellValue("ESourceCode").ToString
        lstCurrentDataSources.ESourceID = cmbSourceName.SelectedRow.GetCellValue("ESourceID")
        lstCurrentDataSources.ESourceName = cmbSourceName.SelectedRow.GetCellValue("ESourceName").ToString
        lstCurrentDataSources.AccessStatus = SourceAccessbility.Neutral
        lstCurrentDataSources.IsND = ldrow("ND").ToString
        lstCurrentDataSources.IsNIL = ldrow("NIL").ToString
        lstCurrentDataSources.ListingID = ldrow("ListingID").ToString
        lstCurrentDataSources.SourceName = ldrow("Column Title").ToString + mstrsingleSpace + ldrow("Listing").ToString
        lstCurrentDataSources.PublicFilingDateCol = Nothing
        lstCurrentDataSources.SourceTypeCode = cmbDataSource.SelectedRow.GetCellValue("SourceTypeCode").ToString
        lstCurrentDataSources.LastUserID = mintProjectID
        lstCurrentDataSources.DataSource = cmbDataSource.Text
        If cmbDataSource.SelectedRow.GetCellValue("SourceTypeCode").ToString = SourceTypeCode.UPD.ToString Or cmbDataSource.SelectedRow.GetCellValue("SourceTypeCode").ToString = SourceTypeCode.DDI.ToString Then
            If cmbSourceName.SelectedRow.GetCellValue("ESourceCode").ToString <> DataSourceCode.DDALCOT.ToString Then
                lstCurrentDataSources.SourceDate = ldrow("Column Title").ToString + mstrsingleSpace + ldrow("Listing").ToString + mstrsingleSpace + mstrDate
            End If
        End If
        mlstNewAddedColumns.Add(lstCurrentDataSources)
    End Sub

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

#End Region
End Class